package com.dhl.fin.api.common.service;

import com.dhl.fin.api.common.domain.ExceptionRecord;
import com.dhl.fin.api.common.util.SpringContextUtil;
import com.dhl.fin.api.common.util.WebUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.PreparedStatement;

/**
 * @author CuiJianbo
 * @since 2025/5/14
 */
@Slf4j
@Service
@Transactional(rollbackFor = Exception.class)
public class ExceptionRecordService extends CommonService<ExceptionRecord> {

    public void addRecordLog(String code, String message, String exception) {
        SqlSessionFactory sqlSessionFactory = SpringContextUtil.getBean(SqlSessionFactory.class);
        String uuid = WebUtil.getLoginUser().getUuid();
        String sql = String.format("insert into t_exception_record(create_time,create_user,update_time,update_user,code,message,business_data) " +
                        "values(now(),'%s',now(),'%s','%s','%s','%s')",
                uuid, uuid, code, message, exception);
        String columnNames[] = new String[]{"id"};
        try {
            PreparedStatement statement = sqlSessionFactory.openSession().getConnection().prepareStatement(sql, columnNames);
            statement.executeUpdate();
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
    }
}
